﻿Imports Business
Imports DTO
Imports System.Globalization

Public Class frmEmployeeDetail

    Dim normalMessage As String = "Format day is 'dd/MM/yyyy'."
    Dim erWrongFormat As String = "Input date is wrong format."
    Dim erExTo1 As String = "Retirement date to must be greater than start date."
    Dim erStartDate1 As String = "Start date must be less than retirement date to."
    Dim strErr1 As String = "Home phone is wrong format."
    Dim strErr2 As String = "Cell phone is wrong format."
    Dim strErr3 As String = "Email is wrong format."
    Dim strErr4 As String = "Date of birth is wrong format."
    Dim strErr5 As String = "Id card is wrong format."
    Dim strMsg1 As String = "Are you sure to cancel the updated data?"
    Dim strMsg2 As String = "Are you sure to want to delete?"

    Dim strErr6 As String = "Delete employee is not successful."
    Dim strErr7 As String = "Employee No cannot be empty."
    Dim strErr8 As String = "Organizational unit cannot be empty."
    Dim strErr9 As String = "Timetable cannot be empty."
    Dim strErr10 As String = "Last name cannot be empty."
    Dim strErr11 As String = "First name cannot be empty."
    Dim strErr12 As String = "Employee is duplicated."
    Dim strErr13 As String = "Add employee is not successful."
    Dim strErr15 As String = "Cannot change organizational unit."
    Dim strErr14 As String = "Cannot change employee no."
    Dim strErr16 As String = "Modify employee is not successful."
    Dim strErr17 As String = "Start date is not valid."

    Dim ID As String

    Dim empNo As String

    Dim dtoCurrEmployee As DTOEmployee

    Dim bsEmployee As BSEmployee

    Dim bsTimetable As BSTimetable

    Dim dtMarStatus As DataTable

    Dim isEdited As Boolean

    Dim yearFromWork As Integer = 2000

    Dim yearToWork As Integer = 2020

    Dim yearFromBirth As Integer = 1953

    Dim yearToBirht As Integer = 1996


    ''' <summary>
    ''' Reg ID from FP terminal 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IdProperty() As String
        Get
            Return ID
        End Get
        Set(ByVal value As String)
            ID = value
        End Set
    End Property

    ''' <summary>
    ''' employee No
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property EmpNoProperty() As String
        Get
            Return empNo
        End Get
        Set(ByVal value As String)
            empNo = value
        End Set
    End Property


    Dim serverName As String

    Public Sub New(ByVal serverName As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.serverName = serverName

        'load combobox organizational unit
        Dim bsOrganization As BSOrganization = New BSOrganization(Me.serverName)

        Functions.FillComboBox(cboOrgan, bsOrganization.GetOrganization())

        ' load combobox timetable
        bsTimetable = New BSTimetable(Me.serverName)

        Functions.FillComboBox(cboTimetable, bsTimetable.GetTimetable0())

        ' dto employee current
        dtoCurrEmployee = New DTOEmployee()

        ' business employee
        bsEmployee = New BSEmployee(Me.serverName)

        ' datatable marital status
        dtMarStatus = New DataTable()

        dtMarStatus.Columns.Add("NO")

        dtMarStatus.Columns.Add("NAME")

        dtMarStatus.Rows.Add("0", "Single")

        dtMarStatus.Rows.Add("1", "Married")

        Functions.FillComboBox(cboMStatus, dtMarStatus)

        lblMessage.Text = normalMessage

    End Sub

    Private Sub frmEmployeeDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' load registration No 
        txtRegNo.Text = ID

        'load employee No if it is not empty
        If Not String.IsNullOrEmpty(empNo) Then

            txtEmpNo.Text = empNo

            ' load all employee details 
            LoadEmpDetails()

        Else

            ' disable button modify, delete
            btnDelete.Enabled = False

            btnModify.Enabled = False

        End If



    End Sub

    ''' <summary>
    ''' Load all employee details 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadEmpDetails()


        dtoCurrEmployee = bsEmployee.GetEmpByIdNo(ID, empNo)

        txtFName.Text = dtoCurrEmployee.FirNameProperty

        txtLName.Text = dtoCurrEmployee.LasNameProperty

        txtHPhone.Text = dtoCurrEmployee.HomPhoneProperty

        txtCPhone.Text = dtoCurrEmployee.CelPhoneProperty

        txtIDCard.Text = dtoCurrEmployee.IdCardProperty

        txtCurAddress.Text = dtoCurrEmployee.CurrAddressProperty

        txtRegAddress.Text = dtoCurrEmployee.RegAddressProperty

        txtPwd.Text = dtoCurrEmployee.LoginPwdProperty

        If Not dtoCurrEmployee.ExpFromProperty.Equals("0") Then

            txtStartDate.Text = Functions.ConvertstrDBDateToStrDate(dtoCurrEmployee.ExpFromProperty)

        End If

        If Not dtoCurrEmployee.ExpToProperty.Equals("99999999") Then

            txtRetDate.Text = Functions.ConvertstrDBDateToStrDate(dtoCurrEmployee.ExpToProperty)

        End If


        If Not dtoCurrEmployee.DatOfBirthProperty Is Nothing Then

            txtDateOfBirth.Text = Functions.ConvertstrDBDateToStrDate(dtoCurrEmployee.DatOfBirthProperty)

        End If
        
        txtEmail.Text = dtoCurrEmployee.EmailProperty

        cboOrgan.SelectedValue = dtoCurrEmployee.OrgNoProperty

        cboTimetable.SelectedValue = dtoCurrEmployee.TimNoProperty

        If Not dtoCurrEmployee.MarStatusProperty Is Nothing Then

            cboMStatus.SelectedValue = dtoCurrEmployee.MarStatusProperty

        End If

        If dtoCurrEmployee.GenderProperty.Equals("1") Then

            rdoMale.Checked = True

        Else

            rdoFemale.Checked = True

        End If

    End Sub

    Private Sub txtStartDate_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStartDate.Enter

        'check if string text in textbox is not empty 
        If Not String.IsNullOrWhiteSpace(txtStartDate.Text) Then

            ' convert string date to integer data
            txtStartDate.Text = ConvertStrDateToInt(txtStartDate.Text)

        End If

    End Sub

    Private Sub txtStartDate_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStartDate.Leave

        If Not String.IsNullOrWhiteSpace(txtStartDate.Text) And txtStartDate.Text.Length >= 7 Then

            ' validate year, month, day
            If Functions.ValidateStrDate(Double.Parse(txtStartDate.Text.Trim()).ToString("0#/##/####"), yearFromWork, yearToWork) Then

                If Not String.IsNullOrEmpty(txtRetDate.Text) Then

                    If DateTime.ParseExact(Double.Parse(txtStartDate.Text.Trim()).ToString("0#/##/####"), "d", New CultureInfo("fr-FR")) _
                        < DateTime.ParseExact(txtRetDate.Text, "d", New CultureInfo("fr-FR")) Then

                        '  set value to textbox
                        txtStartDate.Text = Double.Parse(txtStartDate.Text).ToString("0#/##/####")

                        'set normal message
                        lblMessage.Text = normalMessage

                        'set value isEdited
                        isEdited = True

                    Else

                        ' false clear value
                        txtStartDate.Text = Nothing

                        ' print error message
                        lblMessage.Text = erStartDate1

                    End If

                Else

                    '  set value to textbox
                    txtStartDate.Text = Double.Parse(txtStartDate.Text).ToString("0#/##/####")

                    'set normal message
                    lblMessage.Text = normalMessage

                    'set value isEdited
                    isEdited = True

                End If


            Else

                ' false clear value
                txtStartDate.Text = Nothing

                ' print error message
                lblMessage.Text = erWrongFormat

            End If
        Else

            ' false clear value
            txtStartDate.Text = Nothing

        End If


    End Sub


    Private Sub txtRetDate_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRetDate.Enter

        'check if string text in textbox is not empty 
        If Not String.IsNullOrWhiteSpace(txtRetDate.Text) Then

            ' convert string date to integer data
            txtRetDate.Text = ConvertStrDateToInt(txtRetDate.Text)

        End If

    End Sub


    Private Sub txtRetDate_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRetDate.Leave

        If Not String.IsNullOrWhiteSpace(txtRetDate.Text) And txtRetDate.Text.Length >= 7 Then

            ' validate year, month, day
            If Functions.ValidateStrDate(Double.Parse(txtRetDate.Text.Trim()).ToString("0#/##/####"), yearFromWork, yearToWork) Then

                If Not String.IsNullOrEmpty(txtStartDate.Text) Then

                    If DateTime.ParseExact(Double.Parse(txtRetDate.Text.Trim()).ToString("0#/##/####"), "d", New CultureInfo("fr-FR")) _
                        > DateTime.ParseExact(txtStartDate.Text, "d", New CultureInfo("fr-FR")) Then

                        ' set value to textbox
                        txtRetDate.Text = Double.Parse(txtRetDate.Text).ToString("0#/##/####")

                        'set normal message
                        lblMessage.Text = normalMessage

                        'set value isEdited
                        isEdited = True

                    Else

                        ' false, clear value
                        txtRetDate.Text = Nothing

                        ' print error message
                        lblMessage.Text = erExTo1

                    End If

                Else

                    ' set value to textbox
                    txtRetDate.Text = Double.Parse(txtRetDate.Text).ToString("0#/##/####")

                    'set normal message
                    lblMessage.Text = normalMessage

                    'set value isEdited
                    isEdited = True

                End If


            Else

                ' false clear value
                txtRetDate.Text = Nothing

                ' print error message
                lblMessage.Text = erWrongFormat

            End If
        Else

            ' false clear value
            txtRetDate.Text = Nothing

        End If

    End Sub


    Private Sub txtStartDate_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStartDate.KeyPress

        ' limit length of text less than 8 letters
        txtStartDate.MaxLength = 8

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If
        
    End Sub

    Private Sub txtRetDate_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRetDate.KeyPress

        ' limit length of text less than 8 letters
        txtRetDate.MaxLength = 8

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If


    End Sub

    Private Sub txtDateOfBirth_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDateOfBirth.Enter

        'check if string text in textbox is not empty 
        If Not String.IsNullOrWhiteSpace(txtDateOfBirth.Text) Then

            ' convert string date to integer data
            txtDateOfBirth.Text = ConvertStrDateToInt(txtDateOfBirth.Text)

        End If

    End Sub

    Private Sub txtDateOfBirth_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDateOfBirth.Leave


        If Not String.IsNullOrWhiteSpace(txtDateOfBirth.Text) And txtDateOfBirth.Text.Length >= 7 Then

            ' validate year, month, day
            If Functions.ValidateStrDate(Double.Parse(txtDateOfBirth.Text.Trim()).ToString("0#/##/####"), yearFromBirth, yearToBirht) Then

                txtDateOfBirth.Text = Double.Parse(txtDateOfBirth.Text.Trim()).ToString("0#/##/####")

                ' clear message
                lblMessage.Text = normalMessage

                'set value isEdited
                isEdited = True

            Else

                ' false clear value
                txtDateOfBirth.Text = Nothing

                ' print error message
                lblMessage.Text = strErr4

            End If

        Else

            ' clear value
            txtDateOfBirth.Text = Nothing

        End If

    End Sub

    Private Sub txtDateOfBirth_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDateOfBirth.KeyPress

        ' limit length of text less than 8 letters
        txtDateOfBirth.MaxLength = 8

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If
        
    End Sub

  

    Private Sub txtEmail_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.Leave

        'check if email is not empty
        If Not String.IsNullOrEmpty(txtEmail.Text) Then

            ' validate email
            If System.Text.RegularExpressions.Regex.IsMatch(txtEmail.Text, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$") Then

                ' clear message
                lblMessage.Text = normalMessage

                'set value isEdited
                isEdited = True

            Else

                ' print error wrong format
                lblMessage.Text = strErr3

                ' clear value input
                txtEmail.Text = Nothing

            End If

        Else

            lblMessage.Text = normalMessage

        End If



    End Sub

    Private Sub txtIDCard_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIDCard.KeyPress

        ' limit length of text less than 9 letters
        txtIDCard.MaxLength = 9

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

    Private Sub txtIDCard_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIDCard.Leave

        If Not String.IsNullOrEmpty(txtIDCard.Text) Then

            ' check if string length < 9
            If txtIDCard.Text.Length = 9 Then

                ' clear message
                lblMessage.Text = normalMessage

                'set value isEdited
                isEdited = True

            Else

                ' print erro wong format
                lblMessage.Text = strErr5

                ' clear value
                txtIDCard.Text = Nothing

            End If

        Else

            ' clear message
            lblMessage.Text = normalMessage

        End If

    End Sub

    Private Sub txtHPhone_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtHPhone.KeyPress

        ' limit length of text less than 11 letters
        txtHPhone.MaxLength = 11

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub


    Private Sub txtHPhone_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHPhone.Leave

        If Not String.IsNullOrEmpty(txtHPhone.Text) Then

            ' check string length = 11
            If txtHPhone.Text.Length = 11 Then

                ' clear message
                lblMessage.Text = normalMessage

                'set value isEdited
                isEdited = True

            Else

                ' print error message wrong format
                lblMessage.Text = strErr1

                'clear value
                txtHPhone.Text = Nothing

            End If

        Else

            ' clear message
            lblMessage.Text = normalMessage

        End If


    End Sub

    Private Sub txtCPhone_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCPhone.KeyPress

        ' limit length of text less than 11 letters
        txtCPhone.MaxLength = 11

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If


    End Sub

    Private Sub txtCPhone_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCPhone.Leave

        If Not String.IsNullOrEmpty(txtCPhone.Text) Then

            ' check string length = 10 or 11
            If txtCPhone.Text.Length = 10 Or txtCPhone.Text.Length = 11 Then

                ' clear message
                lblMessage.Text = normalMessage

                'set value isEdited
                isEdited = True

            Else

                ' print error message wrong format
                lblMessage.Text = strErr2

                'clear value
                txtCPhone.Text = Nothing

            End If

        Else

            ' clear message
            lblMessage.Text = normalMessage

        End If
        
    End Sub


  
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        Me.Close()

    End Sub

    Private Sub frmEmployeeDetail_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

        If isEdited Then

            If MessageBox.Show(strMsg1, " Message", MessageBoxButtons.OKCancel) = DialogResult.OK Then

                e.Cancel = False

            Else

                e.Cancel = True

            End If

        Else

            e.Cancel = False

        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        lblMessage.Text = Functions.ConvertstrDBDateToStrDate(dtoCurrEmployee.ExpToProperty)

        If MessageBox.Show(strMsg2, " Message", MessageBoxButtons.OKCancel) = DialogResult.OK Then

            ' check if id and No it not empty
            If Not String.IsNullOrEmpty(txtRegNo.Text) And Not String.IsNullOrEmpty(txtEmpNo.Text) Then

                ' delete Employee
                ' set txtEmpNo to the current employee value
                txtEmpNo.Text = dtoCurrEmployee.EmpNoProperty

                ' check if ex to is default value then delete and change 
                If dtoCurrEmployee.ExpToProperty.Equals("99999999") Then

                    If bsEmployee.DeleteEmployee(ID, empNo, Date.Now.ToString("yyyyMMdd")) Then

                        isEdited = False

                        Me.DialogResult = DialogResult.OK

                        Me.Close()

                    Else

                        'print  error message
                        lblMessage.Text = strErr6
                        
                    End If

                ElseIf DateTime.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoCurrEmployee.ExpToProperty), "d", New CultureInfo("fr-FR")) > Date.Now Then

                    If bsEmployee.DeleteEmployee(ID, empNo, Date.Now.ToString("yyyyMMdd")) Then

                        isEdited = False

                        Me.DialogResult = DialogResult.OK

                        Me.Close()

                    Else

                        'print  error message
                        lblMessage.Text = strErr6

                    End If

                Else
                    
                    ' delete with no change to ex to
                    If bsEmployee.DeleteEmployeeWithoutExTo(ID, empNo) Then

                        isEdited = False

                        Me.DialogResult = DialogResult.OK

                        Me.Close()

                    Else

                        ' print error message
                        lblMessage.Text = strErr6

                    End If

                End If


            End If

            End If

    End Sub


    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click

        'flag 
        Dim flag As Boolean = True

        'employee no not null
        If Not String.IsNullOrEmpty(txtEmpNo.Text) Then

            ' cannot change Employee No, Organization.
            If txtEmpNo.Text.Trim().Equals(dtoCurrEmployee.EmpNoProperty) Then

                ' check organizational unit 
                If cboOrgan.SelectedValue.ToString().Equals(dtoCurrEmployee.OrgNoProperty) Then

                    ' check rest of details
                    flag = ValidateEmployeeDetail()

                Else

                    ' print error message organizational unit cannot change


                    lblMessage.Text = strErr15

                    flag = False

                End If

            Else

                ' print message employee no cannot change 

                lblMessage.Text = strErr14

                flag = False

                txtEmpNo.Focus()

                txtEmpNo.SelectAll()
                
            End If

        Else
            
            ' print message
            lblMessage.Text = strErr7

            ' flag = false
            flag = False

        End If

        If flag Then

            Dim dtoEmployee As DTOEmployee = New DTOEmployee()

            ' get all fields
            GetDetailsEmployeeModify(dtoEmployee)

            ' set no and organinzation unit current
            dtoEmployee.EmpNoProperty = dtoCurrEmployee.EmpNoProperty

            dtoEmployee.OrgNoProperty = dtoCurrEmployee.OrgNoProperty

            If bsEmployee.ModifyEmployee(dtoEmployee) Then

                isEdited = False

                Me.DialogResult = DialogResult.OK

                Me.Close()

            Else

                ' print error message


                lblMessage.Text = strErr16

            End If


        End If


    End Sub

  

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim flag As Boolean = True

        ' check if new employee
        If String.IsNullOrEmpty(empNo) Then

            'check employee no not empty
            If Not String.IsNullOrEmpty(txtEmpNo.Text) Then

                'check employee must not be duplicated
                If Not bsEmployee.CheckDuplicateEmpNo(txtEmpNo.Text.Trim()) Then

                    'check organizational unit
                    If cboOrgan.SelectedIndex > 0 Then

                        ' check rest of details
                        flag = ValidateEmployeeDetail()

                    Else

                        ' print message
                        lblMessage.Text = strErr8

                        flag = False

                    End If

                Else

                    ' print message
                    lblMessage.Text = strErr12

                    flag = False

                    txtEmpNo.Focus()

                    txtEmpNo.SelectAll()

                End If

            Else

                ' print message
                lblMessage.Text = strErr7

                ' flag = false
                flag = False

            End If

            If flag Then

                Dim dtoEmployee As DTOEmployee = New DTOEmployee()

                ' get all fields
                GetDetailsEmployee(dtoEmployee)

                ' add to DB
                If bsEmployee.AddEmployee(dtoEmployee) Then

                    isEdited = False

                    Me.DialogResult = DialogResult.OK

                    Me.Close()

                Else

                    ' print error message
                    lblMessage.Text = strErr13
                    
                End If
                
            End If
            
            ' old employee
        Else

            ' check new start date > current start date
            If Not String.IsNullOrEmpty(txtStartDate.Text) Then

                ' check if start date current employee = 0 then not need to compare
                If Not dtoCurrEmployee.ExpFromProperty.Equals("0") Then
                    
                    ' check new start date  > current employee start date
                    If Not DateTime.ParseExact(txtStartDate.Text, "d", New CultureInfo("fr-FR")) _
                        > DateTime.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoCurrEmployee.ExpFromProperty), "d", New CultureInfo("fr-FR")) Then

                        ' print error message
                        lblMessage.Text = "Start date must be greater than current start date."

                        flag = False

                        txtStartDate.Focus()

                        txtStartDate.SelectAll()

                    End If

                End If

                ' check if start date is valid
                If flag Then

                    'check employee no not empty
                    If Not String.IsNullOrEmpty(txtEmpNo.Text) Then

                        'check organizational unit
                        If cboOrgan.SelectedIndex > 0 Then

                            ' check rest of details
                            flag = ValidateEmployeeDetail()

                        Else

                            ' print message
                            lblMessage.Text = strErr8

                            flag = False

                        End If
                        
                    Else

                        ' print message
                        lblMessage.Text = strErr7

                        ' flag = false
                        flag = False

                    End If

                End If

            Else

                ' print error message
                lblMessage.Text = "Start date cannot empty."

                flag = False

                txtStartDate.Focus()

                txtStartDate.SelectAll()

            End If

            ' check flag
            If flag Then

                ' add to database
                'delete current employee 
                ' delete change ex to is the previous day of new employee's ex from
                Dim exToCurEmp As Date = Date.ParseExact(txtStartDate.Text, "d", New CultureInfo("fr-FR")).AddDays(-1)

                If bsEmployee.DeleteEmployee(dtoCurrEmployee.IdProperty, dtoCurrEmployee.EmpNoProperty, exToCurEmp.ToString("yyyyMMdd")) Then

                    ' add new employee have id same as current employee
                    Dim dtoEmployee As DTOEmployee = New DTOEmployee()

                    GetDetailsEmployee(dtoEmployee)

                    If bsEmployee.AddEmployee(dtoEmployee) Then

                        isEdited = False

                        Me.DialogResult = DialogResult.OK

                        Me.Close()

                    Else

                        ' print error message
                        lblMessage.Text = strErr13

                    End If

                Else

                    ' print error message
                    lblMessage.Text = strErr13

                End If


            End If

        End If


    End Sub


    Private Function ValidateEmployeeDetail() As Boolean

        Dim flag As Boolean = True

       ' check timetable
        If cboTimetable.SelectedIndex > 0 Then

            ' check last name
            If Not String.IsNullOrEmpty(txtLName.Text) Then

                ' check fisrt name
                If Not String.IsNullOrEmpty(txtFName.Text) Then

                    ' clear message
                    lblMessage.Text = normalMessage

                Else

                    ' print message 
                    lblMessage.Text = strErr11

                    flag = False

                End If

            Else

                ' print message
                lblMessage.Text = strErr10

                flag = False

            End If

        Else

            'print message
            lblMessage.Text = strErr9

            flag = False
            
        End If

        Return flag

    End Function


    ''' <summary>
    ''' Get employee details for "add" button
    ''' </summary>
    ''' <param name="dtoEmployee"></param>
    ''' <remarks></remarks>
    Private Sub GetDetailsEmployee(ByRef dtoEmployee As DTOEmployee)

        ' cannot null
        dtoEmployee.IdProperty = ID

        dtoEmployee.EmpNoProperty = txtEmpNo.Text.Trim()

        dtoEmployee.LasNameProperty = txtLName.Text.Trim()

        dtoEmployee.FirNameProperty = txtFName.Text.Trim()

        dtoEmployee.OrgNoProperty = cboOrgan.SelectedValue

        dtoEmployee.TimNoProperty = cboTimetable.SelectedValue

        ' get ex from  and ex to timetable
        bsTimetable.GetExpTimetable(cboTimetable.SelectedValue, dtoEmployee.ExpTimFromProperty, dtoEmployee.ExpTimToProperty)


        If Not String.IsNullOrEmpty(txtRetDate.Text) Then

            dtoEmployee.ExpToProperty = Functions.ConvertStrDateToStrDBDate(txtRetDate.Text)

        Else

            If Not dtoCurrEmployee.ExpToProperty Is Nothing Then

                ' check if ex to of current employee is existed
                If Not dtoCurrEmployee.ExpToProperty.Equals("99999999") Then

                    ' check if ex to > now then apply for new employee
                    If DateTime.Parse(Functions.ConvertstrDBDateToStrDate(dtoCurrEmployee.ExpToProperty)) > Date.Now Then

                        dtoEmployee.ExpToProperty = dtoCurrEmployee.ExpToProperty

                    Else

                        dtoEmployee.ExpToProperty = "99999999"

                    End If

                Else

                    dtoEmployee.ExpToProperty = "99999999"

                End If

            Else

                dtoEmployee.ExpToProperty = "99999999"

            End If


        End If

        If Not String.IsNullOrEmpty(txtStartDate.Text) Then

            dtoEmployee.ExpFromProperty = Functions.ConvertStrDateToStrDBDate(txtStartDate.Text)

        Else

            dtoEmployee.ExpFromProperty = "0"

        End If


        ' option
        ' password
        If Not String.IsNullOrEmpty(txtPwd.Text) Then

            dtoEmployee.LoginPwdProperty = txtPwd.Text

        Else

            dtoEmployee.LoginPwdProperty = DBNull.Value

        End If

        ' date of birth
        If Not String.IsNullOrEmpty(txtDateOfBirth.Text) Then

            dtoEmployee.DatOfBirthProperty = Functions.ConvertStrDateToStrDBDate(txtDateOfBirth.Text)

        Else

            dtoEmployee.DatOfBirthProperty = DBNull.Value

        End If

        ' id card
        If Not String.IsNullOrEmpty(txtIDCard.Text) Then

            dtoEmployee.IdCardProperty = txtIDCard.Text

        Else

            dtoEmployee.IdCardProperty = DBNull.Value

        End If

        ' home phone
        If Not String.IsNullOrEmpty(txtHPhone.Text) Then

            dtoEmployee.HomPhoneProperty = txtHPhone.Text

        Else

            dtoEmployee.HomPhoneProperty = DBNull.Value

        End If

        ' cell phone
        If Not String.IsNullOrEmpty(txtCPhone.Text) Then

            dtoEmployee.CelPhoneProperty = txtCPhone.Text

        Else

            dtoEmployee.CelPhoneProperty = DBNull.Value

        End If

        ' registration address
        If Not String.IsNullOrEmpty(txtRegAddress.Text) Then

            dtoEmployee.RegAddressProperty = txtRegAddress.Text

        Else

            dtoEmployee.RegAddressProperty = DBNull.Value

        End If

        ' current address
        If Not String.IsNullOrEmpty(txtCurAddress.Text) Then

            dtoEmployee.CurrAddressProperty = txtCurAddress.Text

        Else

            dtoEmployee.CurrAddressProperty = DBNull.Value

        End If

        'email
        If Not String.IsNullOrEmpty(txtEmail.Text) Then

            dtoEmployee.EmailProperty = txtEmail.Text

        Else

            dtoEmployee.EmailProperty = DBNull.Value

        End If

        ' marital status
        If cboMStatus.SelectedIndex > 0 Then

            dtoEmployee.MarStatusProperty = cboMStatus.SelectedValue

        Else

            dtoEmployee.MarStatusProperty = DBNull.Value

        End If

        ' gender
        If rdoMale.Checked Then

            dtoEmployee.GenderProperty = "1"

        Else

            dtoEmployee.GenderProperty = "0"

        End If


    End Sub

    ''' <summary>
    ''' Get employee details for "Modify" button
    ''' </summary>
    ''' <param name="dtoEmployee"></param>
    ''' <remarks></remarks>
    Private Sub GetDetailsEmployeeModify(ByRef dtoEmployee As DTOEmployee)

        ' cannot null
        dtoEmployee.IdProperty = ID

        dtoEmployee.EmpNoProperty = txtEmpNo.Text.Trim()

        dtoEmployee.LasNameProperty = txtLName.Text.Trim()

        dtoEmployee.FirNameProperty = txtFName.Text.Trim()

        dtoEmployee.OrgNoProperty = cboOrgan.SelectedValue

        dtoEmployee.TimNoProperty = cboTimetable.SelectedValue

        ' get ex from  and ex to timetable
        bsTimetable.GetExpTimetable(cboTimetable.SelectedValue, dtoEmployee.ExpTimFromProperty, dtoEmployee.ExpTimToProperty)


        If Not String.IsNullOrEmpty(txtRetDate.Text) Then

            dtoEmployee.ExpToProperty = Functions.ConvertStrDateToStrDBDate(txtRetDate.Text)

        Else

            dtoEmployee.ExpToProperty = "99999999"

        End If

        If Not String.IsNullOrEmpty(txtStartDate.Text) Then

            dtoEmployee.ExpFromProperty = Functions.ConvertStrDateToStrDBDate(txtStartDate.Text)

        Else

            dtoEmployee.ExpFromProperty = "0"

        End If


        ' option
        ' password
        If Not String.IsNullOrEmpty(txtPwd.Text) Then

            dtoEmployee.LoginPwdProperty = txtPwd.Text

        Else

            dtoEmployee.LoginPwdProperty = DBNull.Value

        End If

        ' date of birth
        If Not String.IsNullOrEmpty(txtDateOfBirth.Text) Then

            dtoEmployee.DatOfBirthProperty = Functions.ConvertStrDateToStrDBDate(txtDateOfBirth.Text)

        Else

            dtoEmployee.DatOfBirthProperty = DBNull.Value

        End If

        ' id card
        If Not String.IsNullOrEmpty(txtIDCard.Text) Then

            dtoEmployee.IdCardProperty = txtIDCard.Text

        Else

            dtoEmployee.IdCardProperty = DBNull.Value

        End If

        ' home phone
        If Not String.IsNullOrEmpty(txtHPhone.Text) Then

            dtoEmployee.HomPhoneProperty = txtHPhone.Text

        Else

            dtoEmployee.HomPhoneProperty = DBNull.Value

        End If

        ' cell phone
        If Not String.IsNullOrEmpty(txtCPhone.Text) Then

            dtoEmployee.CelPhoneProperty = txtCPhone.Text

        Else

            dtoEmployee.CelPhoneProperty = DBNull.Value

        End If

        ' registration address
        If Not String.IsNullOrEmpty(txtRegAddress.Text) Then

            dtoEmployee.RegAddressProperty = txtRegAddress.Text

        Else

            dtoEmployee.RegAddressProperty = DBNull.Value

        End If

        ' current address
        If Not String.IsNullOrEmpty(txtCurAddress.Text) Then

            dtoEmployee.CurrAddressProperty = txtCurAddress.Text

        Else

            dtoEmployee.CurrAddressProperty = DBNull.Value

        End If

        'email
        If Not String.IsNullOrEmpty(txtEmail.Text) Then

            dtoEmployee.EmailProperty = txtEmail.Text

        Else

            dtoEmployee.EmailProperty = DBNull.Value

        End If

        ' marital status
        If cboMStatus.SelectedIndex > 0 Then

            dtoEmployee.MarStatusProperty = cboMStatus.SelectedValue

        Else

            dtoEmployee.MarStatusProperty = DBNull.Value

        End If

        ' gender
        If rdoMale.Checked Then

            dtoEmployee.GenderProperty = "1"

        Else

            dtoEmployee.GenderProperty = "0"

        End If


    End Sub


End Class